﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using D2D = SharpDX.Direct2D1;
using WIC = SharpDX.WIC;
using DW = SharpDX.DirectWrite;
using DXGI = SharpDX.DXGI;
using System.Windows.Interop;
using SharpDX;
using SharpDX.Mathematics.Interop;

namespace WPFByCoreApp
{
    /// <summary>
    /// Interaction logic for TestSharpDxWindow.xaml
    /// </summary>
    public partial class TestSharpDxWindow : Window
    {
        private D2D.RenderTarget _renderTarget;


        public TestSharpDxWindow()
        {
            InitializeComponent();
            this.Loaded += TestSharpDxWindow_Loaded;
        }



        private void TestSharpDxWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var factory = new D2D.Factory();

            var pixelFormat = new D2D.PixelFormat(DXGI.Format.R8G8B8A8_UNorm, D2D.AlphaMode.Premultiplied);

            var hwndRenderTargetProperties = new D2D.HwndRenderTargetProperties();
            hwndRenderTargetProperties.Hwnd = new WindowInteropHelper(this).Handle;
            hwndRenderTargetProperties.Hwnd = new WindowInteropHelper(this).Handle;
            hwndRenderTargetProperties.PixelSize = new Size2((int)ActualWidth, (int)ActualHeight);

            var renderTargetProperties = new D2D.RenderTargetProperties(D2D.RenderTargetType.Default, pixelFormat,
                96, 96, D2D.RenderTargetUsage.None, D2D.FeatureLevel.Level_DEFAULT);

            _renderTarget = new D2D.WindowRenderTarget(factory, renderTargetProperties, hwndRenderTargetProperties);

            CompositionTarget.Rendering += CompositionTarget_Rendering;

        }

        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            var ellipse = new D2D.Ellipse(new RawVector2(100, 100), 10, 10);

            var brush = new D2D.SolidColorBrush(_renderTarget, new RawColor4(1, 0, 0, 1));
            _renderTarget.BeginDraw();

            _renderTarget.DrawEllipse(ellipse, brush, 1);

            _renderTarget.EndDraw();
        }
    }
}
